package com.eagle.view.fragment.live;

import android.text.TextUtils;
import android.widget.EditText;

import com.eagle.EApplication;
import com.eagle.R;
import com.eagle.support.common.MessageHelper;
import com.eagle.support.constants.Constants;
import com.eagle.support.helper.CallHelper;
import com.eagle.support.utils.SoftInputKeyboardUtils;
import com.eagle.view.activity.LiveShowActivity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.EditorAction;
import org.androidannotations.annotations.ViewById;

/**
 * Created by gc on 2017/1/14.
 */
@EFragment(R.layout.fragment_live_show_operation)
public class FragmentLiveShowOperation extends BaseFragment {

    @ViewById(R.id.et_username)
    protected EditText mEtRoomId;

    protected String mLoginUid;

    private boolean mMaster;

    private LiveShowActivity mActivity;

    private CallHelper mCallHelper;

    @AfterViews
    void initView() {
        mActivity = (LiveShowActivity) getActivity();
        mCallHelper = mActivity.getCallHelper();
        mLoginUid = EApplication.getSharedPreferences().getString(Constants.LOGIN_UID, "");
        mCallHelper.uid(mLoginUid);
    }

    /**
     * 主播开启直播
     */
    @Click(R.id.btn_start)
    void openLiveShowByMaster() {
        mMaster = true;
        showLoading();
        SoftInputKeyboardUtils.hideSoftInput(mEtRoomId);
        MessageHelper.startLive();
    }

    /**
     * 观众加入直播
     */
    @Click(R.id.btn_watching)
    void openLiveShowByAudience() {
        mMaster = false;
        String roomId = mEtRoomId.getText().toString();
        if (!TextUtils.isEmpty(roomId)) {
            showLoading();
            SoftInputKeyboardUtils.hideSoftInput(mEtRoomId);
            mActivity.setRoomId(roomId);
            MessageHelper.startWatch(mLoginUid, roomId);
        }
    }

    @EditorAction(R.id.et_number)
    void onEditorAction() {
        openLiveShowByAudience();
    }

    public boolean isMaster() {
        return mMaster;
    }
}
